package com.ai.module.chat.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author
 * @version 1.0
 */
public class DateUtils {

    public static final String PA_HOUR_sss = "HH:mm:ss.SSS";
    public static final String PA_MONTH_HOUR = "MM-dd_HH_mm_ss_SSS";
    public static final String PA_MONTH_HOUR_LOGIN = "MM-dd@HH-mm-ss-SSS";
    public static final String PA_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String PA_YEAR = "yy-MM";
    public static final String PA_HOUR = "HH:mm:ss";
    public static final Long TIME_MIN =  60 * 1000L;

    public static final Long TIME_HOUR =  60 * 60 * 1000L;
    public static final Long TIME_DAY = 24 * 60 * 60 * 1000L;
    public static final Long WEK_DAY = 7*24 * 60 * 60 * 1000L;
    public static final Long MONTH_DAY = 30*24 * 60 * 60 * 1000L;

    public static final Long HALF_YEAR = 30*24 * 60 * 60 * 1000L;



    /**
     * 获取当前时间(hhmmssSSS)
     *
     * @return
     */
    public static String getCurrentTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PA_DEFAULT);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }

    /**
     * 获取当前时间(MMddhhmmss)
     *
     * @return
     */
    public static String getTransDateTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PA_MONTH_HOUR);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }

    /**
     * 获取当前时间(MMddhhmmss)
     *
     * @return
     */
    public static String getTransDateTimeLogin() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PA_MONTH_HOUR_LOGIN);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }

    public static String getPaYear() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PA_YEAR);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }

    public static String getTimeStr() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PA_HOUR);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }




    /**
     * 判断日期是否为有效日期
     * @param strDate 日期字符串
     * @return
     */
    public static Boolean parse(String strDate, String formatType) {
        if (StringUtils.isBlank(strDate)) {
            return false;
        }
        SimpleDateFormat df = new SimpleDateFormat(formatType);
        try {
            df.setLenient(false);
            df.parse(strDate);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 日期格式转换，将日期转换为指定格式字符串
     * @param date
     * @param type
     * @return
     */
    public static String getDateByType(Date date, String type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
        return simpleDateFormat.format(date);
    }

    public static String parseToString(Date strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(strDate);
    }



    public static int compareDate(long date1, long date2) {
        if (date1 > date2) {
            return 1;
        } else if (date1 < date2) {
            return -1;
        }
        return 0;
    }

    /**
     * 格式化字符串日期(Fri May 18 15:39:46 CST 2018)
     * @param strDate 日期
     * @return
     */
    public static String format(Date strDate) {
        if (strDate == null) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(PA_DEFAULT, Locale.ENGLISH);
        return dateFormat.format(strDate);
    }

    /**
     * 日期加减
     * @param src    起始日期
     * @param type   日期类型(使用Calendar里面的常量)
     * @param amount 数量
     * @return
     */
    public static Date addDate(Date src, int type, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(type, amount);
        return calendar.getTime();
    }




    /**
     * 获取伦敦当前时间(GMT时间)
     * @return
     */
    public static String getLondonCurrentDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(PA_DEFAULT);
        dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/London"));
        return dateFormat.format(new Date());
    }


    /**
     * String转Date类型
     *
     * @param datestr 日期字符串
     * @return
     */
    public static Date stringToDate(String datestr) {

        if (datestr == null || datestr.equals("")) {
            return null;
        }
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(PA_DEFAULT);
        try {
            date = df.parse(datestr);
        } catch (ParseException e) {
            date = DateUtils.stringToDate(datestr, "yyyyMMdd");
        }
        return date;
    }


    /**
     * String转Date类型
     *
     * @param datestr 日期字符串
     * @param dateformat 日期格式
     * @return
     */
    public static Date stringToDate(String datestr, String dateformat) {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(dateformat);
        try {
            date = df.parse(datestr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @return
     */
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));

    }

    public static String format(LocalDateTime dateTime) {
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern(PA_DEFAULT);
        return ofPattern.format(dateTime);
    }
}


